package com.xiaoyi.intentsdklibrary.SDK.Event;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import com.xiaoyi.intentsdklibrary.Utils.MathUtils;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class AlarmClockUtil {
    private static final String TAG = "AlarmClockUtil";
    private static final AlarmClockUtil ourInstance = new AlarmClockUtil();
    private AlarmManager mAm;

    public interface OnAlarmEvenListener {
        void result(int i, String str);
    }

    public static AlarmClockUtil getInstance() {
        return ourInstance;
    }

    private AlarmClockUtil() {
    }

    public void setAlarmOnce(Context context, final int i, String str, String str2, String str3, final String str4, String str5, final OnAlarmEvenListener onAlarmEvenListener) {
        final Calendar instance = Calendar.getInstance();
        instance.set(instance.get(1), instance.get(2), instance.get(5), MathUtils.parseInt(str), MathUtils.parseInt(str2), MathUtils.parseInt(str3));
        if (Calendar.getInstance().getTimeInMillis() > instance.getTimeInMillis()) {
            instance.set(5, instance.get(5) + 1);
        }
        BroadcastReceiver r8 = new BroadcastReceiver() {
            /* class com.xiaoyi.intentsdklibrary.SDK.Event.AlarmClockUtil.AnonymousClass1 */

            public void onReceive(Context context, Intent intent) {
                String stringExtra = intent.getStringExtra("weekFlag");
                String stringExtra2 = intent.getStringExtra("alarMsg");
                if (TextUtils.isEmpty(stringExtra)) {
                    onAlarmEvenListener.result(i, stringExtra2);
                    return;
                }
                Calendar instance = Calendar.getInstance();
                if (stringExtra.contains(instance.get(7) + "")) {
                    onAlarmEvenListener.result(i, stringExtra2);
                }
                Calendar calendar = instance;
                calendar.set(5, calendar.get(5) + 1);
                AlarmClockUtil.this.setMethod(context, i, str4, instance.getTimeInMillis(), stringExtra);
            }
        };
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(context + "AlarmAction" + i);
        context.registerReceiver(r8, intentFilter);
        setMethod(context, i, str4, instance.getTimeInMillis(), str5);
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void setMethod(Context context, int i, String str, long j, String str2) {
        cancelAalrm(context, i);
        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Long.valueOf(j));
        if (this.mAm == null) {
            this.mAm = (AlarmManager) context.getSystemService(NotificationCompat.CATEGORY_ALARM);
        }
        Intent intent = new Intent(context + "AlarmAction" + i);
        intent.putExtra("alarMsg", str);
        intent.putExtra("weekFlag", str2);
        PendingIntent broadcast = PendingIntent.getBroadcast(context, i, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 23) {
            this.mAm.setExactAndAllowWhileIdle(0, j, broadcast);
        } else if (Build.VERSION.SDK_INT >= 19) {
            this.mAm.setExact(0, j, broadcast);
        } else {
            this.mAm.set(0, j, broadcast);
        }
    }

    public void cancelAalrm(Context context, int i) {
        ((AlarmManager) context.getSystemService(NotificationCompat.CATEGORY_ALARM)).cancel(PendingIntent.getBroadcast(context, i, new Intent(context + "AlarmAction" + i), Intent.FLAG_ACTIVITY_NEW_TASK));
    }
}
